﻿<%@ Page Language="C#" Theme="Admin"  MasterPageFile="~/admin/public/admin.master" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Mani.Common" %>

<script runat="server">
	string username;
	
	MembershipUser user;
	
	private void Page_Load()
	{
		username = Request.QueryString["username"];
		if (username == null || username == "")
		{
			Response.Redirect("users.aspx");
		}
		user = Membership.GetUser(username);
		UserUpdateMessage.Text = "";
        
	}
	protected void UserInfo_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
	{
		//Need to handle the update manually because MembershipUser does not have a
		//parameterless constructor  

		user.Email = (string)e.NewValues[0];
		user.Comment = (string)e.NewValues[1];
		user.IsApproved = (bool)e.NewValues[2];

		try
		{
			// Update user info:
			Membership.UpdateUser(user);
			
			// Update user roles:
			UpdateUserRoles();

            //insert the journal information for this user 
            UserUpdateMessage.Text = "Update Successful.";
			e.Cancel = true;
			UserInfo.ChangeMode(DetailsViewMode.ReadOnly);
            
		}
		catch (Exception ex)
		{
			UserUpdateMessage.Text = "Update Failed: " + ex.Message;

			e.Cancel = true;
			UserInfo.ChangeMode(DetailsViewMode.ReadOnly);
		}
	}

   

	private void Page_PreRender()
	{
		// Load the User Roles into checkboxes.
		UserRoles.DataSource = Roles.GetAllRoles();
		UserRoles.DataBind();

		// Disable checkboxes if appropriate:
        if (UserInfo.CurrentMode != DetailsViewMode.Edit)
        {
            foreach (ListItem checkbox in UserRoles.Items)
            {
                checkbox.Enabled = false;

            }
        }
		// Bind these checkboxes to the User's own set of roles.
		string[] userRoles = Roles.GetRolesForUser(username);
       	foreach (string role in userRoles)
		{
			ListItem checkbox = UserRoles.Items.FindByValue(role);
			checkbox.Selected = true;
		}
	}
	
	private void UpdateUserRoles()
	{
		foreach (ListItem rolebox in UserRoles.Items)
		{
			if (rolebox.Selected)
			{
				if (!Roles.IsUserInRole(username, rolebox.Text))
				{
					Roles.AddUserToRole(username, rolebox.Text);
				}
			}
			else
			{
				if (Roles.IsUserInRole(username, rolebox.Text))
				{
					Roles.RemoveUserFromRole(username, rolebox.Text);
				}
			}
		}
	}

	private void DeleteUser(object sender, EventArgs e)
	{
		//Membership.DeleteUser(username, false); // DC: My apps will NEVER delete the related data.
		Membership.DeleteUser(username, true); // DC: except during testing, of course!
		Response.Redirect("users.aspx");
	}

	private void UnlockUser(object sender, EventArgs e)
	{
		// Dan Clem, added 5/30/2007 post-live upgrade.
		
		// Unlock the user.
		user.UnlockUser();
		
		// DataBind the GridView to reflect same.
		UserInfo.DataBind();
	}
    protected string changeDate(DateTime date)
    {
        return date.ToJalali();
    }

   
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="content" Runat="Server">
 <fieldset class="fieldset" >
        <legend>اطلاعات کاربران</legend>
<!-- #include file="_nav.aspx -->

<table >

<td  valign="top">

<h4>نقشها:</h4>
<asp:CheckBoxList  ValidationGroup="vld" ID="UserRoles" runat="server" />

<h4>اطلاعات اصلی:</h4>
<asp:DetailsView AutoGenerateRows="False" DataSourceID="MemberData"
  ID="UserInfo" runat="server" OnItemUpdating="UserInfo_ItemUpdating" 
        CellPadding="4" ForeColor="#333333" GridLines="None" Width="494px"
  >
  
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  
<Fields>
	<asp:BoundField DataField="UserName" HeaderText="نام کاربری" ReadOnly="True" />
	<asp:BoundField DataField="Email" HeaderText="ایمیل" />
	<asp:BoundField DataField="Comment" HeaderText="توضیحات" />
	<asp:CheckBoxField DataField="IsApproved" HeaderText="فعال کردن" />
	<asp:CheckBoxField DataField="IsLockedOut" HeaderText="قفل کردن" ReadOnly="true" />
	<asp:CheckBoxField DataField="IsOnline" HeaderText="آنلاین بودن" ReadOnly="True"  />
	<asp:TemplateField HeaderText="تاریخ ایجاد">
              <ItemTemplate>
               <%#changeDate(Convert.ToDateTime(Eval("CreationDate")))%>
              </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="آخرین ورود">
              <ItemTemplate>
               <%#changeDate(Convert.ToDateTime(Eval("lastlogindate")))%>
              </ItemTemplate>
    </asp:TemplateField>
	 <asp:TemplateField HeaderText="آخرین تغییر رمز عبور">
              <ItemTemplate>
               <%#changeDate(Convert.ToDateTime(Eval("LastPasswordChangedDate")))%>
              </ItemTemplate>
    </asp:TemplateField>
	<asp:CommandField ButtonType="button" ValidationGroup="vld" CancelText="لغو"  UpdateText="بروزرسانی" ShowEditButton="true" EditText="ویرایش اطلاعات کاربر" />
</Fields>
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
<div  style="padding: 5px;">
<asp:Literal ID="UserUpdateMessage"  runat="server">&nbsp;</asp:Literal>
</div>


<div style="text-align: right; width: 100%; margin: 20px 0px;">
<asp:Button ID="Button1" runat="server" Text="باز کردن قفل" OnClick="UnlockUser" OnClientClick="return confirm('Click OK to unlock this user.')" />
    &nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" Text="حذف کاربر" OnClick="DeleteUser" OnClientClick="return confirm('آیا از حذف این کاربر مطمئن هستید?')" />
</div>


<asp:ObjectDataSource ID="MemberData" runat="server" DataObjectTypeName="System.Web.Security.MembershipUser" SelectMethod="GetUser" UpdateMethod="UpdateUser" TypeName="System.Web.Security.Membership">
	<SelectParameters>
		<asp:QueryStringParameter Name="username" QueryStringField="username" />
	</SelectParameters>
</asp:ObjectDataSource> 
</td>

</tr>
</table>


</fieldset>
</asp:Content>